﻿using AutoMapper;
using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using IntelligentAgriculture.Domain.Interface.SheepFileModelInterface;
using Microsoft.Extensions.Logging;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Services
{
    public class BreedingWeighService:IBreedingWeighService
    {


        private readonly IBreedingWeighRepository breedingRe;
        private readonly ILogger<BreedingWeighService> logger;
        private readonly IMapper imapper;
        private readonly ISheepFileModelRepository sheepModelRe;

        public BreedingWeighService(IBreedingWeighRepository breedingRe, ILogger<BreedingWeighService> logger, IMapper imapper, ISheepFileModelRepository sheepModelRe)
        {
            this.breedingRe = breedingRe;
            this.logger = logger;
            this.imapper = imapper;
            this.sheepModelRe = sheepModelRe;
        }


        /// <summary>
        /// 羊只称重列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageOutput<BreedingWeighOutPut>> DepartMnetList(BreedingWeighInPut input)
        {
            try
            {
                var listquery = (from x in await breedingRe.BreedingWeigh()                            
                                 join y in await  sheepModelRe.GetSheepfilemodels()
                                 on x.EarTag equals y.EarTag
                                 where x.Isdelete == 0
                                 select new BreedingWeighOutPut
                                 {
                                     BreedingId = x.BreedingId,
                                     EarTag = y.EarTag,
                                     SheepRescissionSex = x.SheepRescissionSex,
                                     Variety = x.Variety,
                                     WeighCategory = x.WeighCategory,
                                     WeightMonthsAge = x.WeightMonthsAge,
                                     Weight = x.Weight,
                                     WeighDate = x.WeighDate,
                                     CreateName = x.CreateName,
                                     CreateTime = x.CreateTime,
                                     Isdelete = 0,
                                     UpdateName = x.UpdateName,
                                     UpdateTime = x.UpdateTime,

                                 });
                if (!string.IsNullOrEmpty(input.startDate))
                {
                    listquery = listquery.Where(x => x.WeighDate >= DateTime.Parse(input.startDate));

                }
                if (!string.IsNullOrEmpty(input.endDate))
                {
                    listquery = listquery.Where(x => x.WeighDate <= DateTime.Parse(input.endDate));
                }
                if (!string.IsNullOrEmpty(input.earTag))
                {
                    listquery = listquery.Where(x => x.EarTag.Contains(input.earTag));
                }
                if (input.weighCategory > 0)
                {
                    listquery = listquery.Where(x => x.WeighCategory == input.weighCategory);
                }
                if (input.variety > 0)
                {
                    listquery = listquery.Where(x => x.Variety == input.variety);
                }
                if (input.sheepRescissionSex != null)
                {
                    listquery = listquery.Where(x => x.SheepRescissionSex == input.sheepRescissionSex);
                }
                if (input.startAge > 0)
                {
                    listquery = listquery.Where(x => x.WeightMonthsAge >= input.startAge);
                }
                if (input.endAge > 0)
                {
                    listquery = listquery.Where(x => x.WeightMonthsAge <= input.endAge);
                }

                var result = new PageOutput<BreedingWeighOutPut>
                {
                    TotalCount = listquery.Count(),
                    PageSize = input.PageSize,
                };

                listquery = listquery.OrderBy(c => c.BreedingId).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
                result.Data = imapper.Map<List<BreedingWeighOutPut>>(listquery.ToList());
                return result;
            }
            catch (Exception)
            {

                throw;
            }
            
        }



        /// <summary>
        /// 删除羊只称重信息
        /// </summary>
        /// <param name="breedingID"></param>
        /// <returns></returns>
        public async Task<int> Delete(int breedingID)
        {
            return await breedingRe.Delete(breedingID);
        }

        /// <summary>
        /// 添加羊只称重信息
        /// </summary>
        /// <param name="breedingweigh"></param>
        /// <returns></returns>
        public async Task<int> Add(Breedingweigh breedingweigh)
        {
            var entity = imapper.Map<Breedingweigh>(breedingweigh);
            return await breedingRe.Add(entity);
        }



        /// <summary>
        /// 更新羊只称重信息
        /// </summary>
        /// <param name="breedingweigh"></param>
        /// <returns></returns>
        public async Task<int> Update(Breedingweigh breedingweigh)
        {
            var entity= imapper.Map<Breedingweigh>(breedingweigh);
            return await breedingRe.Update(entity);
        }





    }


}
